/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"B1A06BEF-0203-422B-8F24-109646FDC431",variableType:4}
 */
var gUservId = 5;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"D59202A7-3E63-409E-9019-E1769AA54506",variableType:4}
 */
var gUsuarioId = null;


/**
 * Callback method for when solution is opened.
 *
 * @properties={typeid:24,uuid:"E56D0002-7725-49E7-BBE3-75C438F47FDE"}
 */
function onSolutionOpen() {
	application.putClientProperty(APP_UI_PROPERTY.TABLEVIEW_WC_DEFAULT_SCROLLABLE, true);
	verificaCarrito()
}

/**
 * @properties={typeid:24,uuid:"416CA48B-B1E1-4701-8244-0A70DADB53FE"}
 */
function verificaCarrito()
{
	if (globals.gUsuarioId != null)
	{
		if (gusuario_to_carrito_cab.getSize() == 0)
		{
			gusuario_to_carrito_cab.newRecord()
		}
	}	
}
/**
 * @properties={typeid:24,uuid:"1DCFF4F1-69F3-4B44-88A6-8CE1B79F9FC4"}
 */
function calcularTotalesCarrito() {
	var $cab = gusuario_to_carrito_cab
	var $cant = $cab.carrito_cab_to_carrito_dtl.getSize()
	var $total = 0
	
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = $cab.carrito_cab_to_carrito_dtl.getRecord(i)
		$rcd.subtotal 			= $rcd.precio_unitario * $rcd.cantidad
		$rcd.descuento_imp 		= $rcd.subtotal * ($rcd.descuento_porc / 100)
		$rcd.total 				= $rcd.subtotal - $rcd.descuento_imp
		$total += $rcd.total
	}
	$cab.subtotal_1 	= $total
	$cab.descuento_imp 	= $total * $cab.descuento_porc / 100
	$cab.subtotal_2 	= $total - $cab.descuento_imp
	$cab.iva_imp 		= $cab.subtotal_2 * $cab.iva_porc / 100
	$cab.total 			= $cab.subtotal_2 + $cab.iva_imp
}

/**
 * @properties={typeid:24,uuid:"1C92689F-3A4F-4BB3-9F8A-5B34ECB7FDF6"}
 */
function borrarCarrito() {
	var $cab = gusuario_to_carrito_cab
//	$cab.carrito_cab_to_carrito_dtl.deleteAllRecords()	
	$cab.deleteRecord()
}

/**
 * Record pre-delete trigger.
 * Validate the record to be deleted.
 * When false is returned the record will not be deleted in the database.
 * When an exception is thrown the record will also not be deleted in the database but it will be added to databaseManager.getFailedRecords(),
 * the thrown exception can be retrieved via record.exception.getValue().
 *
 * @param {JSRecord<db:/infocom/carrito_cab>} record record that will be deleted
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"B4BD3240-F55D-42A5-8BB9-3922D96F2916"}
 */
function onCarrito_cabRecordDelete(record) {
	return record.carrito_cab_to_carrito_dtl.deleteAllRecords()
}

/**
 * @properties={typeid:24,uuid:"D2EFAB85-D0BB-4B8A-BA6C-7B7855BDE10B"}
 * @AllowToRunInFind
 */
function login(usr, pass) 
{
	/** @type {JSFoundSet<db:/fwk_navigation_infocom/usuarios>} */
	var fs = databaseManager.getFoundSet('fwk_navigation_infocom','usuarios')
	fs.find()
		fs.usuario 		= usr
	fs.newRecord()
		fs.mail_login 	= usr
	var $cant = fs.search()
	if ($cant == 1)
	{
		if (fs.hash_pass == globals.hashPass(pass))
		{
			globals.gUservId 	= fs.userv_id
			globals.gUsuarioId 	= fs.usuario_id
			databaseManager.addTableFilterParam('infocom','fac_cabecera','cliente_id','=',globals.gUsuarioId)
			return true
		}
		else
		{
			globals.gDialog('Error','Contraseña incorrecta','OK','CerrarForm')
			return false
		}
	}
	else
	{	
		globals.gDialog('Error','Usuario no existe','OK','CerrarForm')
		return false
	}
}

/**
 * @param {String} form
 * @properties={typeid:24,uuid:"A7A0BDA3-6583-48FA-A3AF-242D22E18708"}
 */
function show(form) {
	forms.main.elements.tab_forms.removeAllTabs()
	forms.main.elements.tab_forms.addTab(forms[form])
}

/**
 * @param {String} ppass
 * @properties={typeid:24,uuid:"6F031C1C-E09B-4047-B5A7-B37B38DF0802"}
 */
function hashPass(ppass) {
	return utils.stringMD5HashBase64(ppass)
}

/**
 * @properties={typeid:24,uuid:"13132238-19D6-4D0F-BF9F-8A9D3323C655"}
 */
function borrarProducto() {
	forms.carrito_compras_dtl.controller.deleteRecord()
	globals.calcularTotalesCarrito()
}
